package com.shop.prj.dao.Impl;

import com.shop.prj.bean.Feedback;
import com.shop.prj.bean.OrderItem;
import com.shop.prj.dao.OrderItemDao;
import com.shop.prj.utils.JDBCUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class OrderItemDaoImpl implements OrderItemDao {
    @Override
    public int insert(OrderItem orderItem) {
        String sql="INSERT INTO `order_item`(`user_id`,`book_id`,`order_id`,`order_item_price_sum`) VALUES (?,?,?,?)";
        int result=0;
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        connection= JDBCUtils.getConnection();
        try {
            preparedStatement=connection.prepareStatement(sql);

            preparedStatement.setInt(1, orderItem.getUserId());
            preparedStatement.setInt(2,orderItem.getBookId());
            preparedStatement.setInt(3,orderItem.getOrderId());
            preparedStatement.setDouble(4,orderItem.getOrderItemPriceSum());
            result=preparedStatement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                preparedStatement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();

            }
        }
        return result;
    }

//    @Override
//    public int update(OrderItem orderItem) {
//        return 0;
//    }

    @Override
    public int delete(Integer id) {
        String sql = "DELETE FROM `order_item` WHERE `order_time_id` = ?";
        Connection connection = JDBCUtils.getConnection();
        PreparedStatement preparedStatement = null;
        int result = 0;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            result = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                preparedStatement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

//    @Override
//    public OrderItem queryByOrderItemuserId(Long id) {
//        return null;
//    }

    @Override
    public List<OrderItem> queryAll() {
        String sql = "SELECT * FROM `order_item`";

        Connection connection = JDBCUtils.getConnection();
        Statement statement = null;
        ResultSet resultSet = null;
        List<OrderItem> list = new ArrayList<OrderItem>();
        try {
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {

              OrderItem orderItem=new OrderItem();
               orderItem.setOrderItemId(resultSet.getInt(1));
               orderItem.setUserId(resultSet.getInt(2));
               orderItem.setBookId(resultSet.getInt(3));
               orderItem.setOrderId(resultSet.getInt(4));
               orderItem.setOrderItemPriceSum(resultSet.getDouble(5));

                list.add(orderItem);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }
}
